<template>
  <a-modal :maskClosable="false"
           :title="title"
           :width="1024"
           :visible="visible"
           :confirmLoading="confirmLoading"
           @ok="handleOk"
           @cancel="handleCancel"
           cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="validatorRules">
        <a-form-model-item label="设备类型" required prop="deviceType">
          <j-dict-select-tag v-model="model.deviceType" dictCode="device_type" placeholder="请选择设备类型" />
        </a-form-model-item>
        <a-form-model-item label="设备SN" required prop="deviceId" >
          <a-input placeholder="请输入设备SN" v-model="model.deviceId" :disabled="model.id>0"/>
        </a-form-model-item>
        <a-form-model-item label="设备名称" required prop="deviceName">
          <a-input placeholder="请输入设备名称" v-model="model.deviceName" />
        </a-form-model-item>
        <a-form-model-item label="品牌">
          <a-input placeholder="请输入品牌名称" v-model="model.brandName" />
        </a-form-model-item>
        <a-form-model-item label="客户姓名" required prop="patientId">
          <a-select
            show-search
            :show-arrow="false"
            v-model="model.patientId"
            placeholder="请输入客户姓名或手机号查询"
            style="width: 100%"
            :filter-option="false"
            @search="fetchUser"
            @change="handleChange"
          >
            <a-select-option v-for="d in patientDataSource" :key="d.patientId">
              {{ d.realName }}({{ d.mobilePhone }})
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction, getFileAccessHttpUrl } from '@/api/manage'
import JImageUpload from '@/components/jeecg/JImageUpload'

export default {
  name: 'ordStoreInfoModal',
  components: {
    JImageUpload
  },
  data() {
    return {
      title: '操作',
      visible: false,
      isEdit: false,
      model: {},
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 12 }
      },
      confirmLoading: false,
      validatorRules: {
        deviceId: [{ required: true, message: '请输入设备SN!' }],
        deviceType: [{ required: true, message: '请选择设备类型!' }],
        deviceName: [{ required: true, message: '请输入设备名称!' }],
        netType: [{ required: true, message: '请选择设备通讯模式!' }],
        patientId: [{ required: true, message: '请输入客户姓名!' }]
      },
      url: {
        add: '/store/ter/device/info/bind',
        patientSearch: '/his/patient/search'
      },
      patientDataSource: []
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit({})
      this.isEdit = false
      this.model.status = '1'
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.isEdit = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          postAction(that.url.add, that.model).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    getAvatarView: function(avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    fetchUser(text) {
      getAction(this.url.patientSearch, { 'keyword': text }).then((res) => {
        if (res.success) {
          this.patientDataSource = res.result
        }
      })
    },
    handleChange(value) {
      this.model.patientId = value
    }

  }
}
</script>

<style scoped>

</style>
